/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alxbrendmaquinarias.controller;

import com.alxbrendmaquinarias.model.Employee;
import com.alxbrendmaquinarias.service.EmployeeService;
import com.alxbrendmaquinarias.util.faces.FacesUtil;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.servlet.ServletException;
import org.omnifaces.util.Faces;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Max
 */
@Controller
public class LoginController implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    EmployeeService employeeService;

    private String userName;
    private String userPassword;

    private Employee employee = new Employee();

    public void logIn() throws IOException, Exception {
        System.out.println(userName);
        System.out.println(userPassword);
        //MODIFICAR LA CONSULTA
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("user", userName);
        parameters.put("pass", userPassword);
        List<Employee> list = employeeService.list("Employee.findUserPass", parameters);
        System.out.println("Lista: " + list);
        if (list.isEmpty()) {
            FacesUtil.addMessage("Error", "Usuario o contraseña inválido", FacesMessage.SEVERITY_INFO);
        } else {
            Employee empTemp = list.get(0);
            if (empTemp.getRole() == null) {
                FacesUtil.addMessage("ERROR", "No tiene asignado ningún ROL, Contactese con el administrador", FacesMessage.SEVERITY_ERROR);
            } else {
                Faces.setSessionAttribute(FacesUtil.ATTRIBUTE_USER, empTemp);
                Faces.redirect("index.jsf");
            }

        }
    }

    public void logOut() throws ServletException, IOException {
        Faces.getSession().removeAttribute(FacesUtil.ATTRIBUTE_USER);
        Faces.logout();
        Faces.invalidateSession();
        Faces.redirect("login.jsf");
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public Employee getEmployee() {
        employee = (Employee)Faces.getSessionAttribute(FacesUtil.ATTRIBUTE_USER);
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

}
